package com.ruoyi.web.controller.liangshi;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.liangshi.domain.BiBsKscHour;
import com.ruoyi.liangshi.service.IBiBsKscHourService;
import com.ruoyi.liangshi.service.IGdArchShiftService;
import com.ruoyi.liangshi.vo.query.BiBsKscHourAvgTimeChartData;
import com.ruoyi.liangshi.vo.query.BiBsKscHourChartData;
import com.ruoyi.liangshi.vo.query.BiBsKscHourDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author LiuJin
 * @Date: Create in 2023-11-27 15:22
 * @Description: this class is
 */
@RequestMapping("/linagshi/bs-ksc-hour")
@RestController
public class BiBsKscHourController extends BaseController {

    @Autowired
    private IBiBsKscHourService biBsKscHourService;

    @Autowired
    private IGdArchShiftService gdArchShiftService;

    /**
     * 获取小时数据列表
     * @param shopId
     * @param date
     * @return
     */
    @GetMapping("/getBsKscHourList")
    public TableDataInfo getBsKscHourList(@RequestParam(value = "shopId",required = false) Long shopId,@RequestParam(required = false,value = "date")String date) {
        startPage();
        List<BiBsKscHourDataVo> biBsKscHourDataList = biBsKscHourService.getBiBsKscHourDataList(shopId,date);
//        Map<String, GdArchShift> shiftMap = gdArchShiftService.list().stream().collect(Collectors.toMap(item -> item.getId().toString(), obj -> obj));
        for (BiBsKscHourDataVo bsKscHourDataVo : biBsKscHourDataList) {
            Map<String, BigDecimal> itemTypeMaps = new HashMap<>();
            Map<String, BigDecimal> timeOutItemTypeMaps = new HashMap<>();
            List<BiBsKscHour> kscHourList = biBsKscHourService.lambdaQuery()
                    .eq(BiBsKscHour::getBsDate, bsKscHourDataVo.getDate())
                    .eq(BiBsKscHour::getBsHour, bsKscHourDataVo.getHour())
//                    .eq(BiBsKscHour::getShift, bsKscHourDataVo.getShift())
                    .list();
//            GdArchShift gdArchShift = shiftMap.get(bsKscHourDataVo.getShift());
//            bsKscHourDataVo.setShift(gdArchShift.getName());
            //筛选菜品类型，然后对菜品数量进行类型
            for (BiBsKscHour biBsKscHour : kscHourList) {
                switch (biBsKscHour.getItemType()) {
                    case "A":
                        BigDecimal itemAcount = itemTypeMaps.get("A");
                        if(ObjectUtil.isNull(itemAcount)){
                            itemAcount=BigDecimal.ZERO;
                        }
                        itemAcount=itemAcount.add(biBsKscHour.getDishesCount());
                        itemTypeMaps.put("A",itemAcount);
                        if(biBsKscHour.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemAcount = timeOutItemTypeMaps.get("A");
                            if(ObjectUtil.isNull(timeOutItemAcount)){
                                timeOutItemAcount=BigDecimal.ZERO;
                            }
                            timeOutItemAcount=timeOutItemAcount.add(biBsKscHour.getDishesCount());
                            timeOutItemTypeMaps.put("A",timeOutItemAcount);
                        }
                        break;
                    case "B":
                        BigDecimal itemBcount = itemTypeMaps.get("B");
                        if(ObjectUtil.isNull(itemBcount)){
                            itemBcount=BigDecimal.ZERO;
                        }
                        itemBcount=itemBcount.add(biBsKscHour.getDishesCount());
                        itemTypeMaps.put("B",itemBcount);
                        if(biBsKscHour.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemBcount = timeOutItemTypeMaps.get("B");
                            if(ObjectUtil.isNull(timeOutItemBcount)){
                                timeOutItemBcount=BigDecimal.ZERO;
                            }
                            timeOutItemBcount=timeOutItemBcount.add(biBsKscHour.getDishesCount());
                            timeOutItemTypeMaps.put("B",timeOutItemBcount);
                        }
                        break;
                    case "C":
                        BigDecimal itemCcount = itemTypeMaps.get("C");
                        if(ObjectUtil.isNull(itemCcount)){
                            itemCcount=BigDecimal.ZERO;
                        }
                        itemCcount=itemCcount.add(biBsKscHour.getDishesCount());
                        itemTypeMaps.put("C",itemCcount);
                        if(biBsKscHour.getTimeOutType().equals("2")){
                            BigDecimal timeOutItemCcount = timeOutItemTypeMaps.get("C");
                            if(ObjectUtil.isNull(timeOutItemCcount)){
                                timeOutItemCcount=BigDecimal.ZERO;
                            }
                            timeOutItemCcount=timeOutItemCcount.add(biBsKscHour.getDishesCount());
                            timeOutItemTypeMaps.put("C",timeOutItemCcount);
                        }
                        break;
                    default:
                        break;

                }
            }
//            int itemAcount = kscHourList.stream().filter(item -> item.getItemType().equals("A")).mapToInt(item -> item.getDishesCount().intValue()).sum();
//            int itemBcount = kscHourList.stream().filter(item -> item.getItemType().equals("B")).mapToInt(item -> item.getDishesCount().intValue()).sum();
//            int itemCcount = kscHourList.stream().filter(item -> item.getItemType().equals("C")).mapToInt(item -> item.getDishesCount().intValue()).sum();
//            itemTypeMaps.put("A", itemAcount);
//            itemTypeMaps.put("B", itemBcount);
//            itemTypeMaps.put("C", itemCcount);
//            //筛选菜品类型和出餐状态，然后对菜品数量进行类型
//            int itemTimeOutAcount = kscHourList.stream().filter(item -> item.getItemType().equals("A") && item.getTimeOutType().equals("2")).mapToInt(item -> item.getDishesCount().intValue()).sum();
//            int itemTimeOutBcount = kscHourList.stream().filter(item -> item.getItemType().equals("B") && item.getTimeOutType().equals("2")).mapToInt(item -> item.getDishesCount().intValue()).sum();
//            int itemTimeOutCcount = kscHourList.stream().filter(item -> item.getItemType().equals("C") && item.getTimeOutType().equals("2")).mapToInt(item -> item.getDishesCount().intValue()).sum();
//            timeOutItemTypeMaps.put("A", itemTimeOutAcount);
//            timeOutItemTypeMaps.put("B", itemTimeOutBcount);
//            timeOutItemTypeMaps.put("C", itemTimeOutCcount);
            bsKscHourDataVo.setItemTypeMaps(itemTypeMaps);
            bsKscHourDataVo.setTimeOutItemTypeMaps(timeOutItemTypeMaps);
        }
        return getDataTable(biBsKscHourDataList);
    }

    /**
     * 获取小时出餐折线图数据
     * @param archType
     * @param shopId
     * @param date
     * @return
     */
    @GetMapping("/buildHourDataChart")
    public AjaxResult buildHourDataChart(@RequestParam(value = "archType", required = false) String archType,
                                         @RequestParam(required = false,value = "shopId")Long shopId,
                                         @RequestParam(required = false,value = "date")String date) {
        List<BiBsKscHourChartData> bsKscHourChartDataList = biBsKscHourService.getBsKscHourChartDataList(  shopId,date);
        List<BigDecimal> aDishesCount = bsKscHourChartDataList.stream().map(BiBsKscHourChartData::getADishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> bDishesCount = bsKscHourChartDataList.stream().map(BiBsKscHourChartData::getBDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> cDishesCount = bsKscHourChartDataList.stream().map(BiBsKscHourChartData::getCDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> dishesTotalCount = bsKscHourChartDataList.stream().map(BiBsKscHourChartData::getDishesTotalCount).collect(Collectors.toList());
        List<BigDecimal> timeOutDishesCount = bsKscHourChartDataList.stream().map(BiBsKscHourChartData::getTimeOutDishesTotalCount).collect(Collectors.toList());
        List<String> hoursData = bsKscHourChartDataList.stream().map(BiBsKscHourChartData::getHour).collect(Collectors.toList());
        AjaxResult result = success();
        result.put("aDishesCount", aDishesCount);
        result.put("bDishesCount", bDishesCount);
        result.put("cDishesCount", cDishesCount);
        result.put("dishesTotalCount", dishesTotalCount);
        result.put("timeOutDishesCount", timeOutDishesCount);
        result.put("hoursData", hoursData);
        return result;
    }

    /**
     * 获取小时出餐耗时折线图数据
     * @param archType
     * @param shopId
     * @param date
     * @return
     */
    @GetMapping("/buildHourAvgTimeDataChart")
    public AjaxResult buildHourAvgTimeDataChart(@RequestParam(value = "archType", required = false) String archType,
                                                @RequestParam(required = false,value = "shopId")Long shopId,
                                                @RequestParam(required = false,value = "date")String date) {
        List<BiBsKscHourAvgTimeChartData> bsKscHourAvgTimeChartDataList = biBsKscHourService.getBsKscHourAvgTimeChartDataList(shopId,date);
        List<String> hoursData = bsKscHourAvgTimeChartDataList.stream().map(BiBsKscHourAvgTimeChartData::getHour).collect(Collectors.toList());
        List<BigDecimal> avgTotalTime = bsKscHourAvgTimeChartDataList.stream().map(BiBsKscHourAvgTimeChartData::getAvgTotalTime).collect(Collectors.toList());
        List<BigDecimal> avgATotalTime = bsKscHourAvgTimeChartDataList.stream().map(BiBsKscHourAvgTimeChartData::getAvgADishesTime).collect(Collectors.toList());
        List<BigDecimal> avgBTotalTime = bsKscHourAvgTimeChartDataList.stream().map(BiBsKscHourAvgTimeChartData::getAvgBDishesTime).collect(Collectors.toList());
        List<BigDecimal> avgCTotalTime = bsKscHourAvgTimeChartDataList.stream().map(BiBsKscHourAvgTimeChartData::getAvgCDishesTime).collect(Collectors.toList());
        AjaxResult result = success();
        result.put("hoursData", hoursData);
        result.put("avgTotalTime", avgTotalTime);
        result.put("avgATotalTime", avgATotalTime);
        result.put("avgBTotalTime", avgBTotalTime);
        result.put("avgCTotalTime", avgCTotalTime);
        return result;
    }
}
